package cn.iphone.testFirst;

import cn.iphone.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest {
    /**
     * 添加数据到ES
     * @throws Exception
     */
    @Test
    public void testAdd() throws Exception {
        for (int i = 1; i < 100; i++) {
            // 1. 开启ES服务，获取ES连接
            TransportClient client = ESClientUtil.getClient();
            // 2.指定索引、类型、ID
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "" + i);
            // 3.准备添加的数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",i);
            map.put("name",i%2==0?"张敏dog":"cat张");
            map.put("age",i);
            IndexResponse indexResponse = builder.setSource(map).get();
            // 4.打印验证
            System.out.println(indexResponse);
            // 5.关流
            client.close();
        }
    }

    /**
     * 查询数据
     * @throws Exception
     */
    @Test
    public void testGet() throws Exception {
        // 1.开启ES服务，获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取要查询的索引、类型、ID
        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "pet", "1");
        // 3. 打印结果
        // System.out.println(getRequestBuilder.get());
         System.out.println(getRequestBuilder.get().getSource());
         // 4.关流
        client.close();
    }

    /**
     * 修改数据
     * @throws Exception
     */
    @Test
    public void testUpt() throws Exception {
        // 1.开启ES服务，获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取要修改的索引、类型、ID
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");
        // 3 准备修改的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","xxxxxx");
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(map).get();
        // 4.打印验证
        System.out.println(updateResponse);
        // 5.关流
        client.close();
    }

    /**
     * 删除数据
     * @throws Exception
     */
    @Test
    public void testDelete() throws Exception {
        // 1.开启ES服务，获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.指定索引、类型、文档Id，得到预新增处理对象
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "pet", "1");
        DeleteResponse response = builder.get();
        // 3.打印验证
        System.out.println(response);
        // 4.关流
        client.close();
    }

    /**
     * 复杂查询
     * @throws Exception
     */
    @Test
    public void testQuery() throws Exception {
        // 1.开启ES服务，获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.设置要查询的库，得到一个库名对象
        SearchRequestBuilder pethome = client.prepareSearch("pethome");
        pethome.setTypes("pet");
        // 3.设置复杂查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 3.1 DSL查询
        boolQueryBuilder.must(QueryBuilders.matchQuery("name","张敏dog"));
        // 3.2 DSL过滤
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").gte(1).lte(50));
        pethome.setQuery(boolQueryBuilder);
        // 3.3 设置分页
        pethome.setFrom(6);
        pethome.setSize(6);
        // 3.4 排序
        pethome.addSort("age", SortOrder.ASC);
        SearchResponse searchResponse = pethome.get();
        // 注意：两次getHits
        SearchHits searchHits = searchResponse.getHits();
        // 遍历结果
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit documentFields : hits) {
            System.out.println(documentFields.getSourceAsMap());
        }
        // 关流
        client.close();
    }
}
